package com.masblearn;

import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 门面模式
 * TankFrame只对GameModel打交道
 */
public class GameModel {
    private static final  GameModel GAME_MODEL = new GameModel();
    Tank tank =  null;

    ColiderChain chain = new ColiderChain();
//    Colider colider = new BulletTankColider();
//    Colider tankColider = new TankTankColider();

    private List<GameObject> gameModels = new ArrayList<>();

    static {
        GAME_MODEL.init();
    }

    private GameModel() {

    }

    private void init(){
        int initTankCount = Integer.parseInt((String) PropertiesManager.get("initTankCount"));
        for (int i = 0; i < initTankCount; i++) {
            new Tank(50 + i * 80, 200, Dir.DOWN, Group.BAD);
        }

        //主战
        tank = new Tank(200, 200, Dir.UP, Group.GOOD);


        add(new Wall(200, 100, 200, 50));
        add(new Wall(400, 500, 200, 50));
        add(new Wall(780, 320, 50, 200));
        add(new Wall(300, 280, 50, 200));
    }

    public static  GameModel getInstance(){
        return GAME_MODEL;
    }

    public void add(GameObject gameObject) {
        this.gameModels.add(gameObject);
    }

    public void remove(GameObject gameObject) {
        this.gameModels.remove(gameObject);
    }

    public void paint(Graphics g) {
        //画出tank
        Color color = g.getColor();
        g.setColor(Color.white);
        tank.paint(g);

        for (int i = 0; i < gameModels.size(); i++) {
            gameModels.get(i).paint(g);
        }

        //碰撞。仿用comparator
        for (int i = 0; i < gameModels.size(); i++) {
            for (int j = i + 1; j < gameModels.size(); j++) {
                GameObject o1 = gameModels.get(i);
                GameObject o2 = gameModels.get(j);

                chain.colider(o1, o2);
//                colider.colider(o1,o2);
//                tankColider.colider(o1,o2);
            }
        }
//        //碰撞
//        for (int i = 0; i < bulletList.size(); i++) {
//            for (int j = 0; j < tanks.size(); j++) {
//                bulletList.get(i).colliedwith(tanks.get(j));
//            }
//        }

        //不要使用增强for.容易出错
//        for(Bullet bullet :bulletList){
//            bullet.paint(g);
//        }
        //也可以使用Itertor
//        for (int i = 0; i < bulletList.size(); i++) {
//            this.bulletList.get(i).paint(g);
//        }
//        //画敌人坦克
//        for (int i = 0; i < tanks.size(); i++) {
//            tanks.get(i).paint(g);
//        }
//
//        //画敌人坦克
//        for (int i = 0; i < explodes.size(); i++) {
//            explodes.get(i).paint(g);
//        }
//

        //爆炸
//        e.paint(g);
    }

    public Tank getMainTank() {
        return tank;
    }
}
